package com.zkthink.ceres.file.utils;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.tiia.v20190529.TiiaClient;
import com.tencentcloudapi.tiia.v20190529.models.DetectLabelItem;
import com.tencentcloudapi.tiia.v20190529.models.DetectLabelRequest;
import com.tencentcloudapi.tiia.v20190529.models.DetectLabelResponse;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName ImgJudgeUtil
 * @Description: ImgJudgeUtil
 * @Author ck
 * @Date 2022/3/1
 * @Version V1.0
 **/
@Configuration
public class ImgJudgeUtil {


    private static String secretId = "AKIDSxCBtKUN1OVLvDEyReaFdlcCVZMr53lC";

    private static String secretkey = "BBvOaVAYfU4NO8cplQn090avzmIXXFgq";

    private static String region = "ap-beijing";


//    private static String secretId ;
//
//    private static String secretkey;
//
//    private static String region = "ap-beijing";
//
//
//    @Value("${ceres.file.tencent.access-key:}")
//    private String accessKey;
//    @Value("${ceres.file.tencent.secret-key:}")
//    private String secretKey;
//
//
//
//
//    @PostConstruct
//    public void init() {
//        secretId = accessKey;
//        secretkey = secretKey;
//        System.out.println("---腾讯图像识别---" + secretId + "---appSecret---" + secretkey + "---");
//    }

    /**
     * 检测上传头像是否是人物
     *
     * @param url 0是通过1:是送往后台审核2:拒绝
     * @return
     */
    public static int judeHeadImg(String url) {
        try {
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            //图片格式：PNG、JPG、JPEG
            //图片大小：不超过4M。图片下载时间不超过3秒。建议图片大于50 x 50像素，长宽比小于5，否则影响识别效果
            Credential cred = new Credential(secretId, secretkey);
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("tiia.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            TiiaClient client = new TiiaClient(cred, region, clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            DetectLabelRequest req = new DetectLabelRequest();
            req.setImageUrl(url);
            // 返回的resp是一个DetectLabelResponse的实例，与请求对象对应
            DetectLabelResponse resp = client.DetectLabel(req);
            // 输出json格式的字符串回包
            System.out.println(DetectLabelResponse.toJsonString(resp));

            for (DetectLabelItem label : resp.getLabels()) {
                if (label.getFirstCategory().equals("人物") && label.getConfidence() > 80) {
                    return 0;
                }
            }
            for (DetectLabelItem label : resp.getLabels()) {
                if (label.getFirstCategory().equals("人物") && label.getConfidence() <= 80) {
                    return 1;
                }
            }
            return 2;
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
            return 2;
        }
    }

    public static void main(String[] args) {
        System.out.println(judeHeadImg("https://img.xsyaq.com//2022/03/1000000172078582.jpg"));
    }
}
